﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyDoiXe.lop_doi_tuong;
using QuanLyDoiXe.Lop_du_lieu;
using System.IO;
using BarcodeLib;


namespace QuanLyDoiXe
{
    public partial class frmtiepnhandiadiem : Form
    {
        SqlConnection conn = new SqlConnection("Data Source=ACER;Initial Catalog=quanlydoixevinamilk;Integrated Security=True");
        public frmtiepnhandiadiem()
        {
            InitializeComponent();
        }
        #region 1. Thoat & nhap lai
        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnnhaplai_Click(object sender, EventArgs e)
        {
            txtdiachi.Text = "";
            txtemail.Text = "";
            txtnguoilienhe.Text = "";
            txtsdt.Text = "";
            txttenDD.Text = "";
            txttoado.Text = "";
            cbbloaidd.ResetText();
            txtmientiepnhan.Text = "";
            txtMDD.Text = "";
        }
        #endregion
        #region 2. Hien thi du lieu
        ListViewItem item;
        private void HienThiDiaDiem()
        {
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("Select * from diadiem,loaiDD where diadiem.MloaiDD=loaiDD.MloaiDD ", conn);
                SqlDataReader sqlDA = cmd.ExecuteReader();
                int i = 0;
                lvtiepnhandiadiem.View = View.Details;
                lvtiepnhandiadiem.FullRowSelect = true;
                while (sqlDA.Read())
                {
                    i++;
                    item = new ListViewItem(i.ToString());
                    item.SubItems.Add(sqlDA["MDD"].ToString());
                    item.SubItems.Add(sqlDA["Tendiadiem"].ToString());
                    item.SubItems.Add(sqlDA["Tenloai"].ToString());
                    item.SubItems.Add(sqlDA["Diachi"].ToString());
                    item.SubItems.Add(sqlDA["Toado"].ToString());
                    item.SubItems.Add(sqlDA["SDT"].ToString());
                    item.SubItems.Add(sqlDA["Email"].ToString());
                    item.SubItems.Add(sqlDA["Nguoilienhe"].ToString());
                    item.SubItems.Add(sqlDA["Miendiadiem"].ToString());
                    lvtiepnhandiadiem.Items.Add(item);
                }
                cmd.Dispose();
                sqlDA.Close();
                conn.Close();
            }
            catch
            {
            }
        }
        private void lvtiepnhandiadiem_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ListViewItem item = e.Item;
            txtMDD.Text = item.SubItems[1].Text;
            txttenDD.Text = item.SubItems[2].Text;
            cbbloaidd.Text = item.SubItems[3].Text;
            txtdiachi.Text = item.SubItems[4].Text;
            txtsdt.Text = item.SubItems[6].Text;
            txttoado.Text = item.SubItems[5].Text;
            txtnguoilienhe.Text = item.SubItems[8].Text;
            txtemail.Text = item.SubItems[7].Text;
            txtmientiepnhan.Text = item.SubItems[9].Text;
        }
        string MDD, Tendiadiem, Tenloai, Diachi, Toado, Nguoilienhe, Email, Miendiadiem;
        int SDT;
        private void lvtiepnhandiadiem_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < lvtiepnhandiadiem.Items.Count; i++)
            {
                if (lvtiepnhandiadiem.Items[i].Selected)
                {
                    MDD = item.SubItems[1].Text;
                    Tendiadiem = item.SubItems[2].Text;
                    Tenloai = item.SubItems[3].Text;
                    Diachi = item.SubItems[4].Text;
                    SDT = Convert.ToInt32(item.SubItems[6].Text);
                    Toado = item.SubItems[5].Text;
                    Nguoilienhe = item.SubItems[8].Text;
                    Email = item.SubItems[7].Text;
                    Miendiadiem = item.SubItems[9].Text;
                }
            }
        }
        private void frmtiepnhandiadiem_Load(object sender, EventArgs e)
        {
            HienThiDiaDiem();
        }
        #endregion
        #region 3. Xoa
        private void btnxoa_Click(object sender, EventArgs e)
        {
            lvtiepnhandiadiem.Items.Remove(lvtiepnhandiadiem.FocusedItem);
            Lop_diadiemtiepnhan diadiem = new Lop_diadiemtiepnhan(MDD,Tendiadiem,Tenloai,Diachi,Toado,Nguoilienhe,Email,Miendiadiem,SDT);
            if (diadiem.xoa())
            {
                MessageBox.Show("Quá trình xóa địa điểm ra khỏi danh sách thành công !");
                txtMDD.Text = "";
                txttenDD.Text = "";
                cbbloaidd.Text = "";
                txtdiachi.Text = "";
                txttoado.Text = "";
                txtnguoilienhe.Text = "";
                txtmientiepnhan.Text = "";
                txtemail.Text = "";
                txtsdt.Text = "";
            }

            else
                MessageBox.Show("Quá trinh xóa đia điểm ra khỏi danh thất bại !");
            lvtiepnhandiadiem.Items.Clear();
            HienThiDiaDiem();

        }
        #endregion
        #region 4. Cập nhập
        private void btncapnhap_Click(object sender, EventArgs e)
        {
            string strMaloai = "";
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select MloaiDD from loaiDD where Tenloai=N\'" + cbbloaidd.Text + "\'");
            while (dr.Read())
            {
                strMaloai = dr.GetString(0);
            }
            dr.Close(); dr.Dispose();
            lop_ket_noi.huyketnoi();
            if (txtMDD.Text != "" && txttenDD.Text != "" && cbbloaidd.Text != "" && txtdiachi.Text != "" && txttoado.Text != "" && txtnguoilienhe.Text != "" && txtemail.Text != "" && txtmientiepnhan.Text != "" && txtsdt.Text != "")
            {
                Lop_diadiemtiepnhan diadiem = new Lop_diadiemtiepnhan(txtMDD.Text, txttenDD.Text, strMaloai, txtdiachi.Text, txttoado.Text, txtnguoilienhe.Text, txtemail.Text, txtmientiepnhan.Text, Convert.ToInt32(txtsdt.Text));
                if (diadiem.capnhat())
                    MessageBox.Show("Quá trình cập nhật địa điểm thành công!");
                else
                    MessageBox.Show("Quá trình cập nhật địa điểm thất bại!");
            }
            else
                MessageBox.Show("Bạn hãy nhập đầy đủ thông tin địa điểm! ");
            lvtiepnhandiadiem.Items.Clear();
            HienThiDiaDiem();
        }
        #endregion
        
        #region 5. thêm
        private void btnthem_Click(object sender, EventArgs e)
        {
            string strMloai = "";
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select MloaiDD from loaiDD where Tenloai=N\'" + cbbloaidd.Text + "\'");
            while (dr.Read())
            {
               strMloai = dr.GetString(0);
            }
            dr.Close(); dr.Dispose();
            lop_ket_noi.huyketnoi();

            txtMDD.Enabled = true;
            if (txtMDD.Text != "" && txttenDD.Text != "" && cbbloaidd.Text != "" && txtdiachi.Text != "" && txttoado.Text != "" && txtsdt.Text != "" && txtemail.Text != "" && txtnguoilienhe.Text != "" && txtmientiepnhan.Text != "")
            {
                Lop_diadiemtiepnhan diadiem = new Lop_diadiemtiepnhan(txtMDD.Text, txttenDD.Text, strMloai, txtdiachi.Text, txttoado.Text, txtnguoilienhe.Text, txtemail.Text, txtmientiepnhan.Text, Convert.ToInt32(txtsdt.Text));
                if (diadiem.taomoi())
                    MessageBox.Show("Quá trình thêm địa điểm thành công!");
                else
                    MessageBox.Show("Quá trình thêm địa điểm thất bại!");
            }
            else
                MessageBox.Show("Bạn hãy nhập đầy đủ thông tin địa điểm! ");
            lvtiepnhandiadiem.Items.Clear();
            HienThiDiaDiem();
        }
        #endregion

        #region 6. khởi tạo
        string Madiadiem(string ma)
        {
            Lop_lay_du_lieu dl1 = new Lop_lay_du_lieu();
            string tam = "";
            SqlDataReader dr1 = dl1.lay_reader("select MDD from diadiem");
            while (dr1.Read())
                tam = dr1[0].ToString();
            lop_ket_noi.huyketnoi();
            int i = 0;
            i = int.Parse(tam.Substring(2, tam.Length - 2));
            i++;
            if (i < 10) return "DD00" + i.ToString();
            else
                if (i < 100) return "DD0" + i.ToString();
                else return "DD" + i.ToString();
        }
        private void btnkhoitao_Click(object sender, EventArgs e)
        {
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select MDD from diadiem");
            string strtam = "";
            while (dr.Read())
                strtam = dr[0].ToString();
            lop_ket_noi.huyketnoi();
            if (strtam == "") txtMDD.Text = "00000";
            else txtMDD.Text = Madiadiem(strtam);
        }
        #endregion
    }
}
